package com.kcth.sylyg08.rest;

import com.kcht.adp.common.log.Logger;
import com.kcht.adp.common.utils.BeanUtils;
import com.kcht.sylyg08.dao.IContainerDao;
import com.kcht.sylyg08.dao.IContainerHistoricalStatusDao;
import com.kcht.sylyg08.service.IBindingProductsAndLabels;
import com.kcht.sylyg08.service.IGetProductInfoFromMes;
import com.kcth.sylyg08.entity.*;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import java.util.Date;
import java.util.Optional;

/**
 * @Package: com.kcth.sylyg08.rest
 * @Description: 通过MES系统，获取产品信息
 * @Author: lenovo
 * @CreateDate: 2019/5/20 13:00
 */
@Path("/")
@RequestScoped
@Transactional(value=TxType.NOT_SUPPORTED,rollbackOn=Exception.class)
public class GetProductInfo {
    @Context
    private HttpServletRequest request;
    @Inject
    private Logger logger;
    @Inject
    IGetProductInfoFromMes getProductInfoFromMes;
    @Inject
    IBindingProductsAndLabels bindingProductsAndLabels;
    @Inject
    IContainerDao containerDao;
    @Inject
    IContainerHistoricalStatusDao containerHistoricalStatusDao;

    @GET
    @Path("/getProductInfo")
    @Produces("application/json")
    public Object getProductInfo() throws Exception {
        /** 请求上层应用 MES 系统获取产品信息 */
        String realIP = Optional.ofNullable(request.getHeader("X-Real-IP")).orElse(request.getRemoteAddr());
        logger.info(String.format("[%s]=>POST请求地址:%s/%s", realIP, "GetProductInfo", "getProductInfo"));
        logger.info(String.format("[%s]=>POST请求参数:{}", realIP));
        Product ret = getProductInfoFromMes.getProductInfoFromMes();
        logger.info("return =>" + BeanUtils.ignoreBigProperty(ret));
        return ret;

        /** 生成标签的 EPC 码，在数据库中记录对应关系 */
        /*generateCaseNo.getCaseNo();*/


        /** 返回 产品唯一编码， EPC 码 等相关信息 */
    }

    @POST
    @Path("/submitProductInfo")
    @Produces("application/json")
    @Consumes("application/json")
    public Object submitProductInfo(Product product) throws Exception {
        /** 请求上层应用 MES 系统获取产品信息 */
        String realIP = Optional.ofNullable(request.getHeader("X-Real-IP")).orElse(request.getRemoteAddr());
        logger.info(String.format("[%s]=>POST请求地址:%s/%s", realIP, "GetProductInfo", "submitProductInfo"));
        logger.info(String.format("[%s]=>POST请求参数:%s", realIP, BeanUtils.ignoreBigProperty(product)));
        product.setIssueDate(new Date());
        product.setIssuePerson(realIP);
        bindingProductsAndLabels.bindingProductsAndLabels(product);
        Container container = new Container();
        container.setContainerId(product.getEpc());
        container.setContainerType(product.getProductName());
        container.setIsError(false);
        container.setIssueDate(product.getIssueDate());
        container.setIssuePerson(realIP);
        container.setTid(product.getTid());
        containerDao.save(container);
        ContainerHistoricalStatus containerHistoricalStatus = new ContainerHistoricalStatus();
        containerHistoricalStatus.setChangeTime(product.getIssueDate());
        containerHistoricalStatus.setContainerId(product.getEpc());
        containerHistoricalStatus.setContainerStatus("初始发卡");
        containerHistoricalStatus.setStatus(ContainerStatus.初始发卡);
        containerHistoricalStatus.setToBeMoven(false);
        containerHistoricalStatus.setTid(product.getTid());
        containerHistoricalStatus.setLocation(realIP);
        containerHistoricalStatus.setRtnCode(0);
        containerHistoricalStatus.setProductCode(product.getProductCode());
        containerHistoricalStatus.setProductUniqueIdentifier(product.getProductUniqueIdentifier());
        containerHistoricalStatus.setProductName(product.getProductName());
        containerHistoricalStatusDao.save(containerHistoricalStatus);
        product.setMaterials(null);
        logger.info("return =>" + BeanUtils.ignoreBigProperty(product));
        return product;

        /** 生成标签的 EPC 码，在数据库中记录对应关系 */


        /** 返回 产品唯一编码， EPC 码 等相关信息 */
    }
}
